package com.smartwork.common.sms;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.smartwork.common.config.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by Administrator on 2015/10/15 0015.
 */
public class JpushUtil {

    protected static final Logger LOG = LoggerFactory.getLogger(JpushUtil.class);

    private static final String masterSecret = Global.getConfig("jpush.masterSecret");

    private static final String appKey = Global.getConfig("jpush.appKey");

    /**
     * 手机端消息推送
     * @param sendType 1：单个帐号，2：帐号包 3：全体用户
     * @param sendToList 用户账号
     * @param content 内容
     * @param title 标题
     */
    public static void execute(String sendType, List<String> sendToList, String content, String title) {
        PushPayload payload = null;
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
        if("2".equals(sendType) || "1".equals(sendType)){
            for (String tel : sendToList) {
                payload = buildPushObject_android_and_ios(tel, content, title);
                try {
                    PushResult result = jpushClient.sendPush(payload);
                    LOG.info("Got result - " + result);
                } catch (APIConnectionException e) {
                    LOG.error("Connection error. Should retry later. ", e);
                } catch (APIRequestException e) {
                    if("1011".equals(String.valueOf(e.getErrorCode()))){
                        //用户注册,但未进行登录,所以没有绑定该号码，不做任何处理
                        LOG.error("用户注册,但未进行登录! ", e);
                        continue;
                    } else {
                        LOG.error("Error response from JPush server. Should review and fix it. ", e);
                    }
                }
            }
        }

        if("3".equals(sendType)){
            //推送全员,暂不实现  //TODO
        }
    }

    public static PushPayload buildPushObject_android_and_ios(String sendTo, String content, String title) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(sendTo))
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title).build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .setSound("happy")
                                .addExtra("extra_key", "extra_value").build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }
}
